/**
* Created by yyr on 2018/3/16
*已投资基金详情选项卡基金分配
*/
<template>
    <div>
        <div v-if="editData.editStatus">
            <div class="ca-m-t-15">
                <div v-if="editData.editStatus">
                    <div class="ca-title">
                        <h3>分配信息</h3>
                        <div class="ca-pull-right">
                            <el-button type="danger" size="medium" @click="deleteDistribution(editData)">删除</el-button>
                            <el-button type="primary" size="medium" @click="saveEdit(editData)">保存</el-button>
                            <el-button size="medium" @click="cancelEdit">取消</el-button>
                        </div>
                    </div>
                    <el-form label-width="80px" class="ca-m-t-28">
                        <el-form-item label="分配时间">
                            <el-date-picker type="date" 
                                v-model='editData.disTime' 
                                @change="changeDate"
                                placeholder="选择日期" 
                                style="width:200px"
                                 class="ca-m-r-15"></el-date-picker>
                        </el-form-item>
                        <el-form-item label="分配次数">
                            <el-input type="number" class="ca-vertical-align-middle" v-model="editData.frequency" placeholder="选择输入内容" style="width:200px"></el-input>
                        </el-form-item>
                        <el-row v-if="!isMotherFund">
                            <el-col :span="7">
                                <el-form-item label="分配项目">
                                    <el-select v-model="editItemList"
                                               style="width:200px"
                                               filterable placeholder="请选择"
                                               @change="editChangeItem(editItemList,editData)">
                                        <el-option
                                            v-for="item in itemListAll"
                                            :key="item.id"
                                            :label="item.itemName"
                                            :value="item.id">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="15">
                                <el-tag
                                    v-for="item in editData.itemList"
                                    :key="item.id"
                                    :closable="true"
                                    :close-transition="false"
                                    @close="closeEditItem(item)"
                                    class="ca-m-r-10 ca-m-t-8">
                                    {{item.itemName}}
                                </el-tag>
                            </el-col>
                        </el-row>

                        <el-form-item label="分配原因">
                            <el-input type="textarea" :maxlength="60" v-model="editData.reason" placeholder="选择输入内容"></el-input>
                        </el-form-item>
                    </el-form>
                </div>
                <div v-if="editData.editStatus">
                    <table class="ca-table ca-m-t-15">
                        <tr>
                            <th width="25%">合伙人名称</th>
                            <th width="25%">权益占比(%)</th>
                            <th width="30%">分配金额(万元)</th>
                            <th width="8%" class="p-tb-0"><button type="button" class="ca-btn ca-btn-round" @click="pushDistribution()"><i class="fa fa-plus"></i></button></th>
                        </tr>
                        <tr v-for="(list,index1) in editData.historyList" :key="index1">
                            <td>
                                <el-select v-model="list.lpId"
                                           @change="editChangePartner(list,list.lpId)"
                                           filterable placeholder="请选择" style="width:100%;">
                                    <el-option
                                        v-for="item in partnerAllList"
                                        :key="item.lpId"
                                        :label="item.lpName"
                                        :value="item.lpId">
                                    </el-option>
                                </el-select>
                            </td>
                            <td>
                                <el-input v-model="list.interests" disabled></el-input>
                            </td>
                            <td>
                                <el-input v-model="list.amount" type="number" class="ca-vertical-align-middle" placeholder="选择输入内容"></el-input>
                            </td>
                            <td class="p-tb-0"><button type="button" class="ca-btn ca-btn-round" @click="deleteDistributionSingle(list,index1)"><i class="fa fa-remove"></i></button></td>
                        </tr>
                    </table>
                </div>
                <el-row class="ca-title ca-m-15">
                    <h3>附件</h3>
                    <div class="ca-align-right">
                        <csUpload
                            :fileType="fileType"
                            :businessId="editData.id"
                            uploadType="all"
                            :getFileUploadedReturnData="getFileUploadedReturnDataFun"
                        ></csUpload>
                    </div>
                </el-row>
                <div class="ca-file-ul">
                    <el-row class="ca-file-row" :gutter="20" v-for="(file, fileIndex) in editData.fileList" :key="fileIndex">
                        <el-col :span="4" class="upload-time">{{file.uploadTime|moment('YYYY-MM-DD')}}</el-col>
                        <el-col :span="1">
                            <tz-icon-file-type class="ca-m-r-5" :fileType="file.fileName"></tz-icon-file-type>
                        </el-col>
                        <el-col :span="8">
                            <div class="ca-text-over-flow-ellipsis"
                                 @click="previewFileCheck(file.id, file.fileName)">
                                <span class="ca-font-size-16 ca-pointer">{{file.fileName}}</span>
                            </div>
                        </el-col>
                        <el-col :span="4">
                            <div class="ca-text-over-flow-ellipsis">{{userName}}</div>
                        </el-col>
                        <el-col :span="3" class="ca-pull-right ca-align-right">
                            <a class="ca-download ca-m-r-10" @click="downloadFile(file.id)"></a>
                            <el-button class="ca-table-btn" @click="deleteFile(file.id,fileIndex)">
                                <i class="fa fa-times"></i>
                            </el-button>
                        </el-col>
                    </el-row>
                </div>
            </div>
        </div>
    </div>
</template>

<script type="text/ecmascript-6">
    import {api} from '../api';
    import {STATUS_HTTP_SUCCESS, apiHttp} from '__service/csHttp';
    import mined from '__components/file-list/mixin';
    export default {
        mixins: [mined],
        data() {
            return {
                userName: this.$store.state.user.cnName,
                historyListItem: {//点击加号添加一行
                    amount: '',  //分配金额
                    attribute: '',//资金属性
                    distributionId: '',//分配表主id ,
                    id: '',
                    interests: '',//权益
                    lpId: '',//合伙人id
                    lpName: '',//合伙人名称
                    lpType: '',//合伙人类型
                    partnerType: '1',//合伙人类型：1：客户,2:母基金
                },
                itemList: [{
                    disId: '',//分配id
                    id: '',
                    itemId: '',//项目id
                    itemName: '',//项目名称
                }],
                itemListAll: [],//所有的项目
                editItemList: '',//编辑选择项目
                partnerAllList: [],//所有的合伙人
                fileType: '',
            }
        },
        props: {
            editData: {
                type: Object,
                default() {
                    return {}
                }
            },
            editIndex: {
                type: Number,
                default() {
                    return 0
                }
            },
            isMotherFund: {
                type: Boolean,
                default() {
                    return false
                }
            },
        },
        created() {
            this.init();
            this.getPartnerInfo();
        },
        mounted() {
            let fileTypeList = this.$store.state.user.fileFloderDicVoList;
            fileTypeList.map(item=>{
                if (item.name === '基金分配') {
                    this.fileType = item.type;
                }
            })
        },
        methods: {
            //获取项目下拉列表
            init() {
                apiHttp(api.GET_PROJECT).then( ret=>{
                    if (ret.resCode === STATUS_HTTP_SUCCESS) {
                        this.itemListAll = ret.data;
                    }
                })
            },
            //获取合伙人  人的信息
            getPartnerInfo() {
                let query = {
                    name: '',    //模糊搜索的
                    fundId: this.editData.fundId
                }
                apiHttp(api.GET_PARTNER, query).then( ret=>{
                    if (ret.resCode === STATUS_HTTP_SUCCESS) {
                        this.partnerAllList = ret.data;
                        this.partnerAllList.map(item =>{
                            this.editData.historyList.map(val => {
                                if (val.lpId === item.lpId) {
                                    val.interests = item.interests;
                                }
                            })
                        })
                    }
                })
            },
            //修改之后保存
            saveEdit(row) {
                let data = Object.assign({},row);
                delete data.editStatus;
                delete data.show;
                delete data.hide;
                let editFileIdData = [];
                data.fileList.map(item=>{
                    editFileIdData.push(item.id)
                })
                data.historyList.map(item => {
                    delete item.existence;
                    if (!item.amount) {
                        item.amount = 0;
                    }
                })
                let query = {
                    disTime: data.disTime,
                    fileList: editFileIdData,
                    frequency: data.frequency,
                    fundId: data.fundId,
                    historyList: data.historyList,
                    id: data.id,
                    itemList: data.itemList,
                    money: data.money,
                    reason: data.reason
                }
                apiHttp(api.CHANGE_DISTRIBUTION,query).then( ret=>{
                    if (ret.resCode === STATUS_HTTP_SUCCESS) {
                        this.$message({
                            type: 'success',
                            message: '修改成功!',
                            center: true
                        });
                        this.$emit('addRedDot')
                        this.cancelEdit();
                    }
                })
            },
            //删除分配
            deleteDistribution(row) {
                this.$confirm('是否删除此条信息?', '提示', {
                    cancelButtonText: '取消',
                    confirmButtonText: '确定',
                    type: 'warning'
                }).then(async () => {
                    apiHttp(api.DELETE_DISTRIBUTION,{id: row.id}).then( ret=>{
                        if (ret.resCode === STATUS_HTTP_SUCCESS) {
                            this.$message({
                                type: 'success',
                                message: '删除成功!',
                                center: true
                            });
                            this.cancelEdit();
                        }
                    })
                });
            },
            //删除表格内的一行
            deleteDistributionSingle(list,index) {
                //修改时候删除一行
                if (!list.id) {
                    this.editData.historyList.splice(index,1);
                } else {
                    this.$confirm('是否删除此条合伙人信息?', '提示', {
                        cancelButtonText: '取消',
                        confirmButtonText: '确定',
                        type: 'warning'
                    }).then(async () => {
                        apiHttp(api.DELETE_SINGLE_DISTRIBUTION,{id: list.id}).then( ret=>{
                            if (ret.resCode === STATUS_HTTP_SUCCESS) {
                                this.$message({
                                    type: 'success',
                                    message: '删除成功!',
                                    center: true
                                });
                                this.cancelEdit();
                            }
                        })
                    });
                }
            },
            //点击加号添加一行表格
            pushDistribution() {
                this.editData.historyList.push(Object.assign({},this.historyListItem))
            },
            //编辑时候改变合伙人
            editChangePartner(list,listId) {
                this.partnerAllList.map( v=>{
                    if (v.lpId === listId) {
                        list.lpName = v.lpName;
                        list.lpType = v.lpType;
                        list.interests = v.interests;
                    }
                })
            },
            //取消编辑
            cancelEdit() {
                this.$emit('cancelEdit', this.editIndex)
            },
            //编辑时候改变项目
            editChangeItem(newItem,row) {
                let newItemName = '';
                let itemArr = row.itemList.map( v=>{
                    return v.itemId
                })
                if (itemArr.indexOf(newItem) === -1) {
                    this.itemListAll.map( value=>{
                        if (value.id === newItem) {
                            newItemName = value.itemName;
                        }
                    })
                    row.itemList.push({disId: '',id: '',itemId: newItem,itemName: newItemName})
                }
            },
            //编辑时候关闭项目
            closeEditItem(item) {
                this.editData.itemList.splice(this.editData.itemList.indexOf(item),1)
            },
            //添加  文件成功返回的数据
            getFileUploadedReturnDataFun(arr) {
                this.editData.fileList = this.editData.fileList.concat(arr)
            },
             // 更改分配时间
            changeDate(val) {
                this.partnerAllList = [];
                let currentDate = new Date(val).getTime();
                let result = [];
                apiHttp(api.PARTNER_LIST,{fundId: this.$route.params.fundId}).then( ret=>{
                    if (ret.resCode === STATUS_HTTP_SUCCESS) {
                        ret.data.map(item => {
                            if (currentDate >= item.fundPartnerEntity.recordingTime) {
                                result.push(item);
                            } else {
                                result = [];
                            }
                        })
                        if (result.length) {
                            let list = result[0].distHistoryList;
                            for (let i of list) {
                                this.partnerAllList.push({
                                    lpType: i.type,
                                    lpName: i.customerName,
                                    lpId: i.customerId,
                                    attribute: '',
                                    partnerType: i.partnerType,
                                    interests: i.proportion,
                                })
                            }
                            let data = Object.assign({}, {
                                disTime: val,
                                frequency: this.editData.frequency,
                                fundId: this.$route.params.fundId,
                                id: this.editData. id,
                                historyList: this.partnerAllList,
                                itemList: this.editData.itemList,
                                money: this.editData.money,
                                reason: this.editData.reason,
                                fileList: [],
                            })
                            this.$emit('changeData', data, this.editIndex);
                            this.fileDetail = []; //文件清空
                        } else {
                            this.$emit('changeInterests', this.editIndex,val)
                        }
                    }
                })
            },
            // 删除文件
            deleteFile(id, fileIndex) {
                this.$confirm('确认删除此文件?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                    showClose: false
                }).then(async () => {
                    apiHttp(api.DELETE_FILE,{id: id}).then( ret=>{
                        if (ret.resCode === STATUS_HTTP_SUCCESS) {
                            this.$message({
                                type: 'success',
                                message: '删除成功!',
                                center: true
                            });
                            this.editData.fileList.splice(fileIndex, 1);
                        }
                    })
                })
            },
        },
        components: {
            'csUpload': require('__components/upload-btn/index'),//上传文件组件
        },
    }
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
    table td{
        padding: 5px 5px;
        box-sizing: border-box;
    }
</style>
